package com.zh.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zh.entity.User;
import com.zh.service.UserService;
import com.zh.util.dict.CodeMsg;
import com.zh.util.dict.Validator;
import com.zh.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.util.Date;

@RestController
@RequestMapping("/admin")
@Validated
public class AdminUserController extends BaseController{

    @Autowired
    private UserService userService;



    @PostMapping("/getUserList")
    public Result getUserList(User user){
        Page<User> page = getPage();
        return Result.success(userService.selectPage(page,new EntityWrapper<User>()
                .eq("business_id",user.getBusinessId())
                .ne("role","admin")
                .orderBy("create_time")
        ));
    }

    @PostMapping("/getUserDetial")
    public Result getUserDetial(User user,
                                @NotNull(message = "帐号不能为空")
                                String account){
        User tUser = userService.selectById(account);
        if(tUser==null||!tUser.getBusinessId().equals(user.getBusinessId())){
            return Result.failure(CodeMsg.ACCOUNT_NOT_EXIST);
        }
        return Result.success(tUser);
    }



    @PostMapping("/addUser")
    public Result addUser(User user,
                          @NotNull(message = "用户名不能为空")
                          @Pattern(regexp= Validator.REGEX_ACCOUNT,message="用户名格式错误")
                          String account,
                          @NotNull(message = "密码不能为空")
                          @Pattern(regexp= Validator.REGEX_PASSWORD,message="密码格式错误")
                          String password,
                          String remark){

        User tUser = userService.selectById(account);
        if(tUser!=null){
            return Result.failure(CodeMsg.ACCOUNT_EXIST);
        }

        Date date = new Date();
        tUser = new User();
        tUser.setId(account);
        tUser.setBusinessId(user.getBusinessId());
        tUser.setCreateTime(date);
        tUser.setModifiedTime(date);
        tUser.setPassword(password);
        tUser.setRemark(remark==null?"":remark);
        tUser.setRole("user");
        tUser.setStatus(1);

        userService.insert(tUser);

        return Result.success();
    }

    @PostMapping("/editUser")
    public Result editUser(User user,
                          @NotNull(message = "用户名不能为空")
                          @Pattern(regexp= Validator.REGEX_ACCOUNT,message="用户名格式错误")
                                  String account,
                          @NotNull(message = "密码不能为空")
                          @Pattern(regexp= Validator.REGEX_PASSWORD,message="密码格式错误")
                                  String password,
                          String remark,
                          int status){

        User tUser = userService.selectById(account);
        if(tUser==null||!tUser.getBusinessId().equals(user.getBusinessId())){
            return Result.failure(CodeMsg.ACCOUNT_NOT_EXIST);
        }
        tUser.setModifiedTime(new Date());
        tUser.setPassword(password);
        tUser.setRemark(remark==null?"":remark);
        tUser.setStatus(status);
        userService.updateById(tUser);

        return Result.success();
    }

    @PostMapping("/delUser")
    public Result editUser(User user,
                           @NotNull(message = "用户名不能为空")
                           @Pattern(regexp= Validator.REGEX_PASSWORD,message="用户名格式错误")
                                   String account){

        User tUser = userService.selectById(account);
        if(tUser==null||!tUser.getBusinessId().equals(user.getBusinessId())){
            return Result.failure(CodeMsg.ACCOUNT_NOT_EXIST);
        }

        tUser.setStatus(99);//删除
        userService.updateById(tUser);

        return Result.success();
    }
}
